<?php
require_once 'Database.php';
/**
 * Created by JetBrains PhpStorm.
 * User: Ernerst Dey
 * Date: 4/14/12
 * Time: 3:06 PM
 */
class Question extends Database
{
    private $table = 'questions';
    private $level;

    public function save($data) {
        return $this->insert($this->table, $data);
    }

    public function setLevel($level=null) {
        if (!is_null($level) && is_numeric($level)) {
            $this->level = $level;
            return $this;
        }
    }

    public function getLevel() {
        if (is_numeric($this->level)) {
            return $this->level;
        }
    }

    public function find($arguments=false, $all=true) {
        $sql = $arguments;
        if ($this->table == 'questions') {
            if (!$all) {
                if ($arguments) {
                    $sql = ' WHERE `level` = ' . $this->escapeValue($this->getLevel()) . ' ' . $arguments;
                } else {
                    $sql = ' WHERE `level` = ' . $this->escapeValue($this->getLevel());
                }
            }
        }
        return parent::find($this->table, $sql);
    }

    public function update($data) {
        if (isset($data['id'])) {
            $id = $data['id'];
            unset($data['id']);
            return parent::update($this->table, $id, $data);
        }
    }

    public function delete($id) {
        if ($id) {
            return parent::delete($id, $this->table);
        }
    }

    public function getObjectives($questionId=null, $argument=false) {
        if (!is_null($questionId)) {
            $this->table = 'objectives';
            $questionId = $this->escapeValue($questionId);
            $sql = !$argument ? 'WHERE `question_id` = ' . $questionId : 'WHERE `question_id` = ' . $questionId . ' ' . $argument;
            return $this->find($sql);
        } else {
            return false;
        }
    }
}


